package com.yuncheng.spcyApi.utils;

import cn.hutool.core.date.DateUtil;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.util.UUID;

@Component
public class Wkhtmltopdf {
    private static final Logger log = LoggerFactory.getLogger(Wkhtmltopdf.class);

//    @Value("${file.path3}")
    private String toolPath;

    @Resource
    private BusinessConstant bussinessConstant;

    @Value("${file.path2}")
    public String htmlTempPath;

    public String createHtmlFile(String fileValue)   {
        String htmlUrl="";

        try{
            StringBuffer buffer =new StringBuffer();
            buffer.append("<!DOCTYPE html>" +
                    "<html>" +
                    " <head> " +
                    " <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n" +
                    " </head>" +
                    " <body>");
            buffer.append(fileValue);
            buffer.append("<body>" +
                    "</html>");

            File outdirFile = new File(htmlTempPath);
            if (!outdirFile.exists()) {
                outdirFile.mkdirs();
            }

            htmlUrl=htmlTempPath+"\\"+ UUID.randomUUID()+".html";

            PrintStream printStream = new PrintStream(new FileOutputStream(htmlUrl));

            printStream.println(buffer.toString());
        }catch (Exception e){

        }

        return htmlUrl;
    }

    public void htmltopdf(String htmlUrl,String outFilePath,String outFileName) {

        try {
            File outdirFile = new File(outFilePath);
            if (!outdirFile.exists()) {
                outdirFile.mkdirs();
            }

            String pdfName = outFileName+".pdf";
            File outfile = new File(outdirFile,pdfName);
            String outfilestr  = outfile.getAbsolutePath();
            //正式环境
            /*String command =  "wkhtmltopdf http://127.0.0.1/cyxt_gx/ypcygl!printDzbg.do?ids="+bean.getFsid()+" " +outfilestr;
            String[] cmds ={"/bin/sh","-c",command};
            Process pro = Runtime.getRuntime().exec(cmds);*/
            log.info("wkhtmltopdf{}",toolPath);
            String command = toolPath + " --disable-smart-shrinking --dpi 150 --page-size A4 "+htmlUrl+" "+outfilestr;
            Process pro = Runtime.getRuntime().exec(command);

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(pro.getInputStream()));
            try {
                /*String line;
                while ((line=bufferedReader.readLine()) != null){
                    System.out.println(line);
                }*/
                pro.waitFor();
//                System.out.println("生成pdf成功+--+");
            } catch (Exception e) {
                e.printStackTrace();
//                System.out.println("执行失败1！"+e.getMessage());
            }finally{
                bufferedReader.close();
                pro.exitValue();
            }

        } catch (Exception e) {
            e.printStackTrace();
//            System.out.println("执行失败！"+e.getMessage());
        }

    }

    public void toPdfMethod(String fileValue,String fileName){
        String htmlUrl = createHtmlFile(fileValue);
        String nowDay = DateUtil.today(); // 获取当前日期 yyyy-MM-dd
        String newDir = "/"+nowDay;
        htmltopdf(htmlUrl,
                bussinessConstant.YUNCEHNG_PATH_UPLOAD+newDir,
                fileName);
    }

    public static void main(String[] args){
//        String fileValue = "<h1 style=\"text-align: center;\"> &nbsp; &nbsp;</h1><h1 style=\"text-align: center;\"><img src=\"http://172.18.0.162:8080/api/system/common/static/YchEditor/blobid2_1672213575532.png\" alt=\"\" data-href=\"\" style=\"\"></h1><h1 style=\"text-align: center;\">关于拟选派药品GMP检查员的函</h1><p>${data.user.companyName}:</p><p style=\"text-align: justify;\"> &nbsp; &nbsp; &nbsp; &nbsp;根据《药品管理法》、《药品生产监督管理办法》和《药品生产质量管理规范》（2010修订）的要求和自治区局的工作安排，拟请贵单位${data.user.username}同志</p><p>于${data.startTime}至${data.endTime}药品生产企业进行现场检查工作，若因特殊原因不能按时参加，请于${data.startTime}12点前给予答复。</p><p style=\"text-align: justify;\"> &nbsp; &nbsp; &nbsp; &nbsp;望给予大力支持为盼。</p><p> &nbsp; &nbsp; &nbsp; &nbsp;联系人：${data.nowName}；联系电话：0771-585886193。</p><p> </p><p style=\"text-align: right;\"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"background-color: rgb(250, 250, 250);\"> </span><span style=\"color: white; background-color: white;\">$qzwz$</span></p><p style=\"text-align: right;\"> &nbsp;${data.nowTime}</p>";
//        String htmlUrl = createHtmlFile(fileValue);
//
//        htmltopdf(htmlUrl,
//                "C:\\upFiles",
//                "baidu4");
    }

}
